package com.pyip.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyip.reggie.Entity.Dish;
import com.pyip.reggie.common.R;
import com.pyip.reggie.dto.DishDto;
import com.pyip.reggie.service.DishFlavorService;
import com.pyip.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品和口味
     * @param dishDto
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public R<String> addData(@RequestBody DishDto dishDto){
        Boolean flag = dishService.saveWithFlavor(dishDto);
        return flag ? R.success("添加菜品成功") : R.error("添加失败");
    }

    /**
     * 因为页面会展示菜品的种类，要返回的是categoryName，而不是categoryId,要通过categoryId查找categoryName，封装后返回
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping(value="/page",method = RequestMethod.GET)
    public R<Page<DishDto>> getPage(int page, int pageSize, String name){
        return dishService.getPage(page,pageSize,name);
    }

    /**
     * 根据dishId查找dish和dishflavor表，返回信息
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method = RequestMethod.GET)
    public R<DishDto> getByDishId(@PathVariable Long id){
        return dishService.getByDishId(id);
    }

    /**
     * 通过页面传来的信息DishDto,来更新dish和DishFlavor表中的信息
     * @param dishDto
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateDishAndDishFlavor(@RequestBody DishDto dishDto){
        return dishService.updateDishAndDishFlavor(dishDto);
    }

    /**
     * 根据categoryId查找所有dish
     * @param dish
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public R<List<DishDto>> getList(Dish dish){
        return dishService.getAllDishByCategoryId(dish);
    }
}
